package com.wangyadong.hobby.schedule.jobtask.tree.example;

import com.wangyadong.hobby.schedule.jobtask.tree.Position;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2018/10/813:08
 **/
public abstract class AbstractBinaryTree<E> extends AbstractTree implements BinaryTree<E>{


    @Override
    public Iterable sbling(Position position) throws IllegalArgumentException {
        List<Position> list = new ArrayList<>();
        if (null != left(position)) {
            list.add(right(position));
        }
        if (null != right(position)) {
            list.add(left(position));
        }
        return list;
    }

    @Override
    public int childNum(Position position) throws IllegalArgumentException {
        int count = 0;
        if (null != left(position)) {
            ++count;
        }
        if (null != right(position)) {
            ++count;
        }
        return count;
    }

    @Override
    public Iterable<Position> child(Position position) {
        List<Position> list = new ArrayList<>();
        if (null != left(position)) {
            list.add(left(position));
        }
        if (null != right(position)) {
            list.add(right(position));
        }
        return list;
    }
}
